<script setup lang="ts">
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { User, Lock, UserFilled } from "@element-plus/icons-vue";
import type { FormInstance } from "element-plus";
import useUserStore from "../stores/userStore";
import http from "../utils/http";
import { roles } from "../utils/menu.js";
import config from "../config/index.js";

const userStore = useUserStore();
const router = useRouter();
const loginFormRef = ref<FormInstance>();
const loading = ref(false);

const loginForm = reactive({
  username: "caiya",
  password: "123",
  role: "admin", // 默认选择管理员角色
});

const loginRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  role: [{ required: true, message: "请选择角色", trigger: "change" }],
};

const handleLogin = async () => {
  if (!loginFormRef.value) return;

  try {
    await loginFormRef.value.validate();
    loading.value = true;

    if (loginForm.role == "admin") {
      const res = await http.post("/admin/login", loginForm);
      if (res.code == 200) {
        userStore.setToken(res.data.token),
            userStore.setUserInfo(res.data),
            userStore.setRoleFlag(loginForm.role),
            ElMessage.success("登录成功");
        await router.push("/home"); // 修改为管理端首页
      } else if (res.code === 520) {
        ElMessage.error("无权限");
      }
    } else {
      const res = await http.post("/login", loginForm);
      if (res.code == 200) {
        userStore.setToken(res.data.token),
            userStore.setUserInfo(res.data),
            userStore.setRoleFlag(loginForm.role),
            ElMessage.success("登录成功");
        await router.push("/front/home");
      } else if (res.code === 520) {
        ElMessage.error("无权限");
      }
    }


  } catch (error) {
    console.error("登录失败:", error);
    ElMessage.error("登录失败，请检查用户名和密码");
  } finally {
    loading.value = false;
  }
};

// 跳转到注册页面
const goToRegister = () => {
  router.push("/register");
};
</script>

<template>
  <div class="login-container">
    <!-- 左侧背景图片区域 -->
    <div class="login-left">
      <div class="left-content">
        <div>
          <img src="@/assets/logo.png" alt="logo" class="logo">
        </div>
        <h1 class="system-title">非物质文化遗产保护平台</h1>
        <p class="system-slogan">传承千年匠心·守护文化瑰宝</p>
        <div class="category-items">
          <div class="category-item">
            <span class="category-icon">🎭</span>
            <span class="category-name">戏曲艺术</span>
            <p class="category-desc">传承传统表演艺术的精髓</p>
          </div>
          <div class="category-item">
            <span class="category-icon">🎨</span>
            <span class="category-name">工艺美术</span>
            <p class="category-desc">匠人手工技艺的传承与发展</p>
          </div>
          <div class="category-item">
            <span class="category-icon">🔨</span>
            <span class="category-name">传统技艺</span>
            <p class="category-desc">古老工艺的保护与创新</p>
          </div>
        </div>
        <div class="feature-stats">
          <div class="stat-item">
            <span class="stat-number">1000+</span>
            <span class="stat-label">非遗项目</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">500+</span>
            <span class="stat-label">传承人</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">30+</span>
            <span class="stat-label">省市覆盖</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 右侧登录表单 -->
    <div class="login-right">
      <div class="form-container">
        <div class="login-header">
          <h2 class="form-title">欢迎登录</h2>
          <div class="login-subtitle">非物质文化遗产保护平台</div>
        </div>

        <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            label-width="0"
            size="large"
        >
          <el-form-item prop="username">
            <el-input
                v-model="loginForm.username"
                placeholder="账号"
                :prefix-icon="User"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="密码"
                :prefix-icon="Lock"
                show-password
                @keyup.enter="handleLogin"
            />
          </el-form-item>

          <el-form-item prop="role">
            <el-select
                v-model="loginForm.role"
                placeholder="角色类型"
                :prefix-icon="UserFilled"
            >
              <el-option
                  v-for="role in roles"
                  :key="role.value"
                  :label="role.label"
                  :value="role.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
                type="primary"
                :loading="loading"
                class="login-button"
                @click="handleLogin"
            >
              登录系统
            </el-button>
          </el-form-item>
        </el-form>

        <div class="form-footer">
          <div class="register-link">
            <span>还没有账号？</span>
            <el-button type="text" @click="goToRegister">立即注册</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 背景样式 */
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  width: 100%;
  height: 100vh;
  z-index: 2000;
  overflow: hidden;
}

/* 左侧背景图片区域 */
.login-left {
  width: 60%;
  background: url("/public/images/bg.jpg") no-repeat center center;
  background-size: cover;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.login-left::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
}

.left-content {
  position: relative;
  z-index: 1;
  color: white;
  text-align: center;
}

.system-title {
  font-size: 36px;
  margin-bottom: 15px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.system-slogan {
  font-size: 18px;
  margin-bottom: 40px;
  opacity: 0.9;
}

.category-items {
  display: flex;
  justify-content: center;
  gap: 30px;
  margin-bottom: 40px;
  flex-wrap: wrap;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  max-width: 200px;
}

.category-icon {
  font-size: 32px;
  margin-bottom: 10px;
}

.category-name {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 5px;
}

.category-desc {
  font-size: 14px;
  opacity: 0.8;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.feature-stats {
  display: flex;
  justify-content: center;
  gap: 40px;
  margin-top: 40px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-number {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 16px;
  opacity: 0.9;
}

/* 右侧登录表单 */
.login-right {
  width: 40%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: white;
  padding: 40px;
}

.form-container {
  width: 100%;
  max-width: 350px;
  padding: 20px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-title {
  font-size: 24px;
  color: #303133;
  margin-bottom: 8px;
  font-weight: 600;
  position: relative;
  display: inline-block;
}

.form-title::after {
  content: '🏮';
  margin-left: 8px;
  font-size: 24px;
}

.login-subtitle {
  font-size: 16px;
  color: #666;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  border-radius: 8px;
  margin-top: 20px;
  background-color: #ff4757;
  border-color: #ff4757;
}

.login-button:hover {
  background-color: #ff3838;
  border-color: #ff3838;
}

.form-footer {
  margin-top: 30px;
  text-align: center;
}

.register-link {
  font-size: 14px;
  color: #666;
}

.register-link span {
  margin-right: 8px;
}

.register-link .el-button--text {
  color: #ff4757;
}

/* 输入框样式优化 */
:deep(.el-input__wrapper) {
  height: 48px;
  border-radius: 8px;
  box-shadow: none;
  border: 1px solid #e0e0e0;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: none;
  border-color: #d0d0d0;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: none;
  border-color: #ff4757;
}

:deep(.el-input__inner) {
  height: 100%;
  font-size: 15px;
}

:deep(.el-input__prefix-inner) {
  margin-right: 10px;
}

/* 下拉框样式优化 */
:deep(.el-select) {
  width: 100%;
}

:deep(.el-select .el-input__wrapper) {
  height: 48px;
  border-radius: 8px;
  box-shadow: none;
  border: 1px solid #e0e0e0;
}

:deep(.el-select .el-input__wrapper:hover) {
  box-shadow: none;
  border-color: #d0d0d0;
}

:deep(.el-select .el-input__wrapper.is-focus) {
  box-shadow: none;
  border-color: #ff4757;
}

:deep(.el-select .el-input__inner) {
  height: 100%;
  font-size: 15px;
}

/* 动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.login-container {
  animation: fadeIn 0.5s ease-out;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .login-container {
    flex-direction: column;
  }

  .login-left,
  .login-right {
    width: 100%;
  }

  .login-left {
    height: 40%;
    padding: 20px;
  }

  .system-title {
    font-size: 28px;
  }

  .feature-stats {
    gap: 20px;
  }

  .stat-number {
    font-size: 24px;
  }

  .login-right {
    height: 60%;
  }
}

@media (max-width: 576px) {
  .system-title {
    font-size: 24px;
  }

  .system-slogan {
    font-size: 16px;
  }

  .feature-stats {
    flex-direction: column;
    gap: 10px;
  }

  .form-container {
    padding: 10px;
  }
}
</style>
